﻿Public Class DIAG

    Dim connect As CONDB = CONDB.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DIAG_Load(sender As Object, e As EventArgs) Handles Me.Load
        SelectDatagridDiagcode()

        DesignForm(Me)
        DesignGrid(MASDIAGCODEGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))

        ICDEXP.Format = DateTimePickerFormat.Custom
        ICDEXP.CustomFormat = " "
    End Sub

    Private Sub SelectDatagridDiagcode()
        Dim objSelectDataDiagcode As New DIAGCODE
        Dim Diagcode As DataTable
        Diagcode = connect.GetTable(objSelectDataDiagcode.SelectDiag_)
        MASDIAGCODEGrid.PrimaryGrid.DataSource = Diagcode
    End Sub

    Private Sub cleardata()
        ICDCODETxt.Text = ""
        ICDDESC_ENTxt.Text = ""
        ICDDESC_THTxt.Text = ""
        Dim sqldate As String
        Dim dt As New DataTable
        sqldate = " SELECT CURDATE() "
        dt = connect.GetTable(sqldate)
        ICDEXP.Value = Convert.ToDateTime(dt.Rows(0)(0))
        ICDGRPTxt.Text = ""
        Masdiagcodestatuscheck.Checked = False
        F_CHRONIC.Checked = False
        F_IMP.Checked = False
        F_INFEC.Checked = False
        ICDIDTxt.Text = ""
        ICDTYPETxt.Text = ""
    End Sub

    Private Sub loaddate()

    End Sub

    Private Sub ICDTYPETxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ICDTYPETxt.KeyPress
        If ICDTYPETxt.Text.Length > 10 And Not Char.IsControl(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub

    Private Sub ICDGRPTxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ICDGRPTxt.KeyPress
        If ICDGRPTxt.Text.Length > 0 And Not Char.IsControl(e.KeyChar) Then
            e.KeyChar = ""
        End If
        e.KeyChar = e.KeyChar.ToString.ToUpper
    End Sub

    Private Sub MASDIAGCODEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASDIAGCODEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'MsgBox(CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("billid").Value())
        If grid.Cells("icdid").Value() IsNot DBNull.Value Then
            ICDIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("icdid").Value()
        Else
            ICDIDTxt.Text = ""
        End If
        If grid.Cells("รหัส ICD").Value() IsNot DBNull.Value Then
            ICDCODETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส ICD").Value()
        Else
            ICDCODETxt.Text = ""
        End If
        If grid.Cells("ชื่อสากล").Value() IsNot DBNull.Value Then
            ICDDESC_ENTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสากล").Value()
        Else
            ICDDESC_ENTxt.Text = ""
        End If
        If grid.Cells("ชื่อไทย").Value() IsNot DBNull.Value Then
            ICDDESC_THTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อไทย").Value()
        Else
            ICDDESC_THTxt.Text = ""
        End If
        If grid.Cells("ประเภท ICD").Value() IsNot DBNull.Value Then
            ICDTYPETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเภท ICD").Value()
        Else
            ICDTYPETxt.Text = ""
        End If
        If grid.Cells("รหัสกลุ่ม ICD").Value() IsNot DBNull.Value Then
            ICDGRPTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม ICD").Value()
        Else
            ICDGRPTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่ยกเลิก").Value() IsNot DBNull.Value Then
            ICDEXP.Value = Convert.ToDateTime(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่ยกเลิก").Value())
            ICDEXP.Format = DateTimePickerFormat.Custom
            ICDEXP.CustomFormat = "dd/MM/yyyy"
        Else
            ICDEXP.Format = DateTimePickerFormat.Custom
            ICDEXP.CustomFormat = " "
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            Masdiagcodestatuscheck.Checked = True
        Else
            Masdiagcodestatuscheck.Checked = False
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f_chronic").Value() = 1 Then
            F_CHRONIC.Checked = True
        Else
            F_CHRONIC.Checked = False
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f_infec").Value() = 1 Then
            F_INFEC.Checked = True
        Else
            F_INFEC.Checked = False
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f_imp").Value() = 1 Then
            F_IMP.Checked = True
        Else
            F_IMP.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masdiagcode SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `icdid` = '" & ICDIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If

        addMasdiagcode.Enabled = False
    End Sub

    Private Sub addMasdiagcode_Click_1(sender As Object, e As EventArgs) Handles addMasdiagcode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If ICDTYPETxt.Text.Length > 0 And ICDGRPTxt.Text.Length > 0 Then
                    Dim objinsertdata As New DIAGCODE
                    objinsertdata.ICDCODE_ = ICDCODETxt.Text
                    objinsertdata.ICDDESC_EN_ = ICDDESC_ENTxt.Text
                    objinsertdata.ICDDESC_TH_ = ICDDESC_THTxt.Text
                    If ICDEXP.CustomFormat <> " " Then
                        objinsertdata.ICDEXP_ = DateAdd(DateInterval.Year, -543, ICDEXP.Value).ToString("yyyy/MM/dd")
                    End If
                    objinsertdata.ICDGRP_ = ICDGRPTxt.Text
                    objinsertdata.ICDTYPE_ = ICDTYPETxt.Text
                    objinsertdata.STATUS_ = Masdiagcodestatuscheck.Checked
                    objinsertdata.F_CHRONIC_ = F_CHRONIC.Checked
                    objinsertdata.F_IMP_ = F_IMP.Checked
                    objinsertdata.F_INFEC_ = F_INFEC.Checked

                    objinsertdata.insertDiagcodeData()

                    SelectDatagridDiagcode()

                    cleardata()

                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasdiagcode_Click_1(sender As Object, e As EventArgs) Handles editMasdiagcode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If ICDIDTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New DIAGCODE
                        objeditData.ICDID_ = ICDIDTxt.Text
                        objeditData.ICDCODE_ = ICDCODETxt.Text
                        objeditData.ICDDESC_EN_ = ICDDESC_ENTxt.Text
                        objeditData.ICDDESC_TH_ = ICDDESC_THTxt.Text
                        objeditData.ICDGRP_ = ICDGRPTxt.Text
                        If ICDEXP.CustomFormat <> " " Then
                            objeditData.ICDEXP_ = DateAdd(DateInterval.Year, -543, ICDEXP.Value).ToString("yyyy/MM/dd")
                        End If
                        objeditData.STATUS_ = Masdiagcodestatuscheck.Checked
                        objeditData.F_CHRONIC_ = F_CHRONIC.Checked
                        objeditData.F_IMP_ = F_IMP.Checked

                        objeditData.editDiagcodedata()

                        SelectDatagridDiagcode()

                        cleardata()

                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addMasdiagcode.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("masdiagcode")
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL("masdiagcode")
        nextform.ShowDialog()
    End Sub

End Class